package ATM.src;


import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class accountOperator {
    private ArrayList<account> accounts = new ArrayList<>();
    private ArrayList<String> CarIds = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);

    public accountOperator() {
    }

    public void start() {
        System.out.println("===================ATM===================");
        System.out.println("Welcome to the Account Operator!");
        System.out.println("1、用户登录");
        System.out.println("2、用户开户");
        System.out.println("3、退出系统");
        System.out.print("请选择你要的操作：");
        int input = sc.nextInt();
        switch (input) {
            case 1:
                int num = login();
                userUI(num);
                break;
            case 2:
                createAccount();
                break;
            case 3:
                break;
            default:
                System.out.println("请输入正确的数字");
                start();
        }
    }

    public void showCarIds() {
        for (int i = 0; i < CarIds.size(); i++) {
            System.out.println("test");
            System.out.println(CarIds.get(i) + "这个是列表");
        }
    }

    public String createCarId() {
        Random rand = new Random();
        char[] data = new char[7];
        for (int i = 0; i < data.length; i++) {
            data[i] = (char) (rand.nextInt(57 - 48 + 1) + 48);
        }
        String NewCarId = new String(data);
        if (CarIds.size() > 0) {
            for (int i = 0; i < CarIds.size(); i++) {
                if (CarIds.get(i).equals(NewCarId)) {
                    NewCarId = createCarId();
                }
            }
            CarIds.add(NewCarId);
            return NewCarId;
        } else {
            CarIds.add(NewCarId);
            return NewCarId;
        }
    }

    public void createAccount() {
        System.out.println("===================账户开户===================");
        account a = new account();

        System.out.println("请输入姓名：");
        a.setName(sc.next());

        System.out.println("请输入密码：");
        String fristPass = sc.next();

        System.out.println("请确认密码：");
        String secondPass = sc.next();

        while (!fristPass.equals(secondPass)) {
            System.out.println("两次密码输入有误，请重新输入");
            System.out.println("请输入密码：");
            fristPass = sc.next();
            System.out.println("请确认密码：");
            secondPass = sc.next();
        }
        a.setPassword(fristPass);

        a.setCarId(createCarId());
        accounts.add(a);


        System.out.println("创建成功！您的用户名为" + a.getName() + "您的卡号为：" + a.getCarId());
        System.out.println("===================开户成功===================");

        start();

    }

    public void createAccount(String name, String password) {

        accounts.add(new account(createCarId(), name, password));
    }

    public void createAccount(String name, String password, int money) {
        accounts.add(new account(createCarId(), name, password, money));
    }

    public void showAllAccounts() {
        for (int i = 0; i < accounts.size(); i++) {
            account get = accounts.get(i);
            System.out.print(i + "、");
            System.out.println(get.getName() + " " + get.getPassword() + " " + get.getCarId() + " " + get.getMoney());
        }
    }

    public int login() {
        System.out.println("===================登录===================");
        int num = 0;
        if (accounts.size() == 0) {
            System.out.println("当前还没有账户存在系统中");
            start();
        } else {
            System.out.print("请输入登录卡号：");
            String CarId = sc.next();
            num = eraseCarId(CarId);
            if (num >= 0) {
                eraseUserPass(num, CarId);
            } else {
                System.out.println("此卡号不存在请重新输入");
                login();
            }
        }
        return num;

    }

    public void userUI(int num) {
        System.out.println("=================== " + accounts.get(num).getName() + " ===================");
        System.out.println("=================== 余额：" + accounts.get(num).getMoney() + " ===================");
        System.out.println("1、存钱");
        System.out.println("2、取钱");
        System.out.println("3、重设密码");
        System.out.println("4、注销账户");
        System.out.println("5、退出登录");
        switch (sc.next()) {
            case "1":
                storage(num);
                break;
            case "2":
                takeMoney(num);
                break;
            case "3":
                setUserPass(num);
                break;
            case "4":
                removeUser(num);
                break;
            case "5":
                login();
                break;

        }
    }

    public void eraseUserPass(int num, String CarId) {
        System.out.print("请输入密码：");
        String password = sc.next();
        if (accounts.get(num).getCarId().equals(CarId) && accounts.get(num).getPassword().equals(password)) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败请重新输入账号密码");
            eraseUserPass(num, CarId);
        }
    }

    public int eraseCarId(String CarId) {
        for (int i = 0; i < accounts.size(); i++) {
            account get = accounts.get(i);
            if (get.getCarId().equals(CarId)) {
                return i;
            }
        }
        return -1;
    }

    public void storage(int num) {
        System.out.println("输入你要存入的金额");
        int money = sc.nextInt();
        accounts.get(num).setMoney(accounts.get(num).getMoney() + money);
        System.out.println("储存成功");
        userUI(num);
    }

    public void takeMoney(int num) {
        System.out.println("输入你要取出的金额");
        int money = sc.nextInt();
        accounts.get(num).setMoney(accounts.get(num).getMoney() - money);
        System.out.println("取出成功");
        userUI(num);
    }

    public void setUserPass(int num) {
        System.out.println("请输入旧密码");
        String oldPass = sc.next();
        if (accounts.get(num).getPassword().equals(oldPass)) {
            System.out.println("请输入新密码");
            accounts.get(num).setPassword(sc.next());
        } else {
            System.out.println("您不是不本人！将自动退出账户");
            start();
        }
    }

    public void removeUser(int num) {
        System.out.println("请输入密码");
        if (accounts.get(num).getPassword().equals(sc.next())) {
            System.out.println("=================== 删除账户 ===================");
            System.out.println("1、确认");
            System.out.println("2、取消并返回");
            System.out.println("请确认是否删除账户：");
            if (sc.next().equals("1")) {
                accounts.remove(num);
                start();
            } else {
                userUI(num);
            }

        }
    }
}
